﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLogic;

namespace BusinessLogic
{
    [Serializable()]
    public class PartList : System.Collections.CollectionBase
    {
        public Part this[int index]
        {
            get { return (Part)this.List[index]; }
            set { this.List[index] = value; }
        }

        public int IndexOf(Part item)
        {
            return base.List.IndexOf(item);
        }

        public int Add(Part item)
        {
            return this.List.Add(item);
        }

        public void Remove(Part item)
        {
            this.InnerList.Remove(item);
        }

        public void CopyTo(Array array, int index)
        {
            this.List.CopyTo(array, index);
        }

        public void AddRange(PartList collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                this.List.Add(collection[i]);
            }
        }

        public void AddRange(Part[] collection)
        {
            this.AddRange(collection);
        }

        public bool Contains(Part item)
        {
            return this.List.Contains(item);
        }

        public void Insert(int index, Part item)
        {
            this.List.Insert(index, item);
        }

        public string CreateXMLString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<PARTS>");

            int sequence = 0;

            foreach (Part p in this.List)
            {
                sb.Append("<ITEM>");    
                sb.AppendFormat("<ID>{0}</ID>", p.ID);            
                sb.AppendFormat("<DESCRIPTION>{0}</DESCRIPTION>", p.Description);
                sb.AppendFormat("<PARTNUMBER>{0}</PARTNUMBER>", p.Number);
                sb.AppendFormat("<PRICE>{0}</PRICE>", p.Price);
                sb.AppendFormat("<QUANTITY>{0}</QUANTITY>", p.Quantity);
                sb.AppendFormat("<SEQUENCE>{0}</SEQUENCE>", sequence++);
                sb.Append("</ITEM>");
            }

            return sb.Append("</PARTS>").ToString();
        }

        public void Fill(Guid serviceReportID)
        {
            this.AddRange(PartListDataAccess.GetServiceReportPartList(serviceReportID));
        }
    }
}